package com.cloudfun.campusshare.repo;

import com.cloudfun.campusshare.common.constant.LoanStatus;
import com.cloudfun.campusshare.entity.LoanEntity;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.Collection;
import java.util.List;

/**
 * Created by Huqin on 2020/3/4
 */
public interface LoanEntityRepo extends
        PagingAndSortingRepository<LoanEntity, String>,
        JpaSpecificationExecutor<LoanEntity> {

    LoanEntity findByIdAndBorrowerId(String id, String userId);

    LoanEntity findByIdAndLenderId(String id, String userId);

    @Query("select sum(l.loanAmount) from LoanEntity l where l.lenderId=?1 and l.status in (?2)")
    Double sumLoanAmountByLenderIdAndStatusIn(String lenderId, Collection<LoanStatus> loanStatus);

    Long countDistinctBorrowerIdByLenderIdAndStatusIsNot(String lenderId, LoanStatus loanStatus);

    Long countByLenderIdAndStatus(String lenderId, LoanStatus loanStatus);

    Long countByLenderIdAndStatusIn(String lenderId, Collection<LoanStatus> loanStatuses);

    List<LoanEntity> findAllByIdInAndStatusNotOrderByCreateTimeDesc(Collection<String> ids, LoanStatus status);

    List<LoanEntity> findAllByStatusNotOrderByCreateTimeDesc(LoanStatus status);
}
